// 导入路由规则
import { constantRoutes, asyncRoutes } from '@/router/index'
const state = {
  // 因为不管是哪个账号 都要静态路由 所以把静态路由作为默认值
  routes: constantRoutes
}
const mutations = {
  // 修改state中的routes
  setRoutes (state, newRoutes) {
    state.routes = [...constantRoutes, ...newRoutes]
  }
}
const actions = {
  getRoutes (context, menus) {
    // 过滤
    let routes = asyncRoutes.filter(item => menus.includes(item.name))
    // 添加404页面
    routes.push({ path: '*', redirect: '/404', hidden: true })

    context.commit('setRoutes', routes)
    // 外界可以得到一个返回值
    return routes
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
